package com.cn.order.tcc;

import com.cn.order.entity.Order;
import com.cn.order.mapper.OrderMapper;
import io.seata.rm.tcc.api.BusinessActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
@Component
public class OrderTCCActionImpl implements OrderTCCAction {
    @Autowired
    private OrderMapper orderMapper;
    @Transactional
    @Override
    public boolean prepare(BusinessActionContext ctx, Long id, Long userId, Long productId, Integer count, BigDecimal money, Integer status) {
//        插入冻结订单
        Order order=new Order(id,userId,productId,count,money,0);
        orderMapper.create(order);
        return true;
    }
    @Transactional
    @Override
    public boolean commit(BusinessActionContext context) {
        /*
        * ctx上下文向第二段传递时，会先转成json，再从json转为上下文对象*/
         Long orderId=Long.valueOf(context.getActionContext("id").toString());
//        设置成正常状态
        orderMapper.updateStatus(orderId,1);
        return true;
    }
    @Transactional
    @Override
    public boolean rollback(BusinessActionContext context) {
//        删除订单
        return false;
    }
}
